package cn.com.headfree.dao.impl;

import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.springframework.stereotype.Repository;

import com.gta.edu.sdk.base.dao.impl.BaseDaoImpl;
import com.gta.edu.sdk.common.dto.Page;
import com.gta.edu.sdk.common.dto.Sort;
import com.gta.edu.sdk.util.IdGen;
import com.gta.edu.sdk.util.StringUtil;

import cn.com.headfree.dao.ITaskDao;
import cn.com.headfree.entity.TCommonTask;
import cn.com.headfree.entity.TCommonTaskExecuteRecord;
import org.mybatis.spring.support.SqlSessionDaoSupport;

@Repository("taskDao")
public class TaskDaoImpl extends BaseDaoImpl<TCommonTask> implements ITaskDao {
	private static final int RECORD_COUNT = 1000;
	@Override
	public void find(Page<TCommonTask> page, Sort sort, TCommonTask task) {
		StringBuilder sql = new StringBuilder("select * from t_common_task t where t.del_sign=0 ");
		List<Object> params = new ArrayList<Object>(1);
		if(task != null){
			if(!StringUtil.isNullOrEmpty(task.getSysCode())){
				sql.append("and t.sys_code = ?");
				params.add(task.getSysCode());
			}
		}
		this.findAsPageBySql(TCommonTask.class, sql.toString(), page, sort, true, params.toArray());
	}

	@Override
	public void deleteTask(String deleteIds) {
		this.executeSql("delete from t_common_task where id in (" + deleteIds + ")", new HashMap<String, Object>());
	}

	@Override
	public void writeRecord(List<TCommonTaskExecuteRecord> recordList) throws Exception {
		String temp = "insert into t_common_task_execute_record(id,task_code,execute_result,execute_error_message,execute_time) values";
		StringBuilder sql = null;
		Map<String, Object> params = new HashMap<String, Object>(recordList.size());
		// 循环提交记录次数
		int forNum = recordList.size() % RECORD_COUNT > 0 ? recordList.size() / RECORD_COUNT + 1 : recordList.size() / RECORD_COUNT;
		// 得到循环次数，每次提交100条记录
		int start = 0;
		int end = 0;
		for (int i = 0; i < forNum; i++) {
			sql = new StringBuilder(temp);
			start = i * RECORD_COUNT;
			end = (i + 1) * RECORD_COUNT - 1;
			if (i == forNum - 1) {
				end = recordList.size() - 1;
			}
			for (int j = start; j <= end; j++) {
				sql.append("(#{id").append(j).append("},#{taskCode").append(j).append("},#{executeResult").append(j)
						.append("},#{executeErrorMessage").append(j).append("},#{executeTime").append(j).append("})");
				if (j != end) {
					sql.append(",");
				}
				params.put("id" + j, IdGen.uuid());
				params.put("taskCode" + j, recordList.get(j).getTaskCode());
				params.put("executeResult" + j, recordList.get(j).getExecuteResult());
				params.put("executeErrorMessage" + j, recordList.get(j).getExecuteErrorMessage());
				params.put("executeTime" + j, recordList.get(j).getExecuteTime());
			}
			this.executeSql(sql.toString(), params);
		}
	}

	@Override
	public void find(Page<TCommonTaskExecuteRecord> page, Sort sort, TCommonTaskExecuteRecord record,Date startDate,Date endDate) {
		StringBuilder sql = new StringBuilder("select * from t_common_task_execute_record t where 1=1 ");
		List<Object> params = new ArrayList<Object>(4);
		if(record != null){
			if(!StringUtil.isNullOrEmpty(record.getTaskCode())){
				sql.append("and t.task_code = ? ");
				params.add(record.getTaskCode());
			}
			if (record.getExecuteResult() != null && !record.getExecuteResult().equals(-1)) {
				sql.append("and t.execute_result = ? ");
				params.add(record.getExecuteResult());
			}
			if (startDate != null) {
				sql.append("and t.execute_time > ? ");
				params.add(startDate);
			}
			if (endDate != null) {
				sql.append("and t.execute_time < ? ");
				params.add(endDate);
			}
		}
		this.findAsPageBySql(TCommonTaskExecuteRecord.class, sql.toString(), page, sort, false, params.toArray());
	}

}
